<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

define('START', true);	

session_start();	
	
# set error checking levels #	
	# enable these lines when site is to be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');
	
# include some needed files #
	# include some needed files #
	$absRoot = '../../';
	include($absRoot . 'lib/XAJAX/xajax_core/xajax.inc.php');
	
# instantiate predifined classes #											
	$xajax = new xajax();

	$xajax->registerFunction("changePassword");
	
	function changePassword($old, $new, $renew) {
		$response = new xajaxResponse();
		
		# check if the fields are empty
			if (empty($old) || empty($new) || empty($renew)) {
				$response->assign('changePasswordFormConfirmation', 'innerHTML', 'Please complete all fields.<br /> Not updating..');
			}
			else {
				# prepare the values
					$old = isset($old) ? trim(md5($old)) : '';
					$new = isset($new) ? trim(md5($new)) : '';
					$renew = isset($renew) ? trim(md5($renew)) : '';
					$agentId = isset($_SESSION['agentId']) ? $_SESSION['agentId'] : '';
					
				
				# include AgentHandle.php and instanciate class
					include('../../src/AgentHandle.php');
					$agent = new AgentHandle();
					$result = $agent->getAgentPassword($_SESSION['agentId']);
			
				# check if the old password entered is the correct password, this is to avoid unauthorized change of password
				# check if the old and new password are the same, if the same do not update
				# check if the new and the confirmation password are the same, if not the same do not update
					if (strcmp($result['agent_password'], $old) != 0) {
						$response->assign('changePasswordFormConfirmation', 'innerHTML', 'Old password is incorrect.<br /> Not updating..');
					}
					elseif (strcmp($new, $old) == 0) {
						$response->assign('changePasswordFormConfirmation', 'innerHTML', 'Old and New Passwords are the same.<br /> Not updating..');
					}
					elseif (strcmp($new, $renew) != 0) {
						$response->assign('changePasswordFormConfirmation', 'innerHTML', 'New Passwords are not the same.<br /> Not updating..');
					}
					else {
						if ($agentId == '') {
							$response->assign('changePasswordFormConfirmation', 'innerHTML', 'No agent ID.<br /> Not updating..');
						}
						else {
							
							$result = $agent->setProfileValue('agent_password', $new, $agentId);
							
							if ($result == true) {
								$response->assign('changePasswordFormConfirmation', 'innerHTML', 'Password changed. <br /> Database updated.');
								$response->assign('oldPassword', 'value', '');
								$response->assign('newPassword', 'value', '');
								$response->assign('renewPassword', 'value', '');
							}
							else {
								$response->assign('changePasswordFormConfirmation', 'innerHTML', $result . '<br />Update error.<br /> Not updating..');
							}
						}
						
					}	
			}
		return$response;
	}
	$xajax->processRequest();	

	//print_r($result);
?>
<html>
<head>	
	<script type="text/javascript" src="../../lib/XAJAX/xajax_js/xajax_core.js" />
	<link rel="stylesheet" type="text/css" href="../../lib/YUI/yui/build/fonts/fonts-min.css" />
	<?php $xajax->printJavaScript(); ?>
</head>

<body class="yui-skin-sam">
<strong>Password Change:</strong>
	<form action="#" method="post" id="changePasswordForm">
		<input type="password" id="oldPassword" /> Old Password <br />
		<input type="password" id="newPassword" /> New Password <br />
		<input type="password" id="renewPassword" /> Re-type New Password <br />
		<input type="button" value="Change Password" onclick="xajax_changePassword(document.getElementById('oldPassword').value, document.getElementById('newPassword').value, document.getElementById('renewPassword').value)" />
	</form>
	<p id="changePasswordFormConfirmation"></p>
</body>
</html>
